package com.facemox.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.facemox.bean.PermissionAuthentication;
import com.facemox.service.AuthorizationService;
import com.facemox.service.ModuleService;

@RestController
public class AuthorizationController {
    
    @Autowired
    private AuthorizationService authorizationService;
    
    @Autowired
    private PermissionAuthentication permissionAuthentication;
    
    @Autowired
    private ModuleService moduleService;
    
    // 添加权限
    @RequestMapping("add_authorization")
    public List<PermissionAuthentication> addAuthorization(HttpServletRequest request) {
        String authorizationType = request.getParameter("authorization_type");
        String authorizationName = request.getParameter("authorization_name");
        String accessTokenUrl = request.getParameter("access_token_url");
        String token = request.getParameter("token");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String remark = request.getParameter("remark");
        
        permissionAuthentication.setAuthenticationType(authorizationType);
        permissionAuthentication.setAuthorizationName(authorizationName);
        permissionAuthentication.setAccessTokenUrl(accessTokenUrl);
        permissionAuthentication.setUsername(username);
        permissionAuthentication.setPassword(password);
        permissionAuthentication.setToken(token);
        permissionAuthentication.setRemark(remark);
        
        int effectLine = authorizationService.addAuthorization(permissionAuthentication);
        
        List<PermissionAuthentication> authenticationList = moduleService.getAllAuthorization();
        
        return authenticationList;
    }
    
    // 查询所有权限
    @RequestMapping("find_authorization")
    public List<PermissionAuthentication> findAuthorization() {
      List<PermissionAuthentication> authenticationList = authorizationService.findAuthorization();
      return authenticationList;
    }
    
    // 根据权限认证ID删除一条权限
    @RequestMapping("delete_authorization_by_id")
    public int deleteAuthorizationById(HttpServletRequest request) {
        String tempAID = request.getParameter("authenticationId");
        Integer authenticationId = Integer.valueOf(tempAID);
        int effectLine = authorizationService.deleteAuthorizationById(authenticationId);
        return effectLine;
    }
    
}
